package com.google.code.netbuilder.dal.providers;

import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Service for getting data providers with particular implementation 
 * example: ep = (IEquipmentProvider) pm.getProvider("Equipment", "JPA");
 * Assumes default bean names are used for data providers
 * @author Sergey Kozlov
 * 
 * Further implementation: default impl value that stored in file.properties 
 *
 */

@Stateless
public class ProviderManager implements IProviderManager{

	@Override
	public Object getProvider(String entity, String impl) {
		String name = entity + "Provider" + impl;
		InitialContext context;
		Object res = null;
		try {
			context = new InitialContext();
			//res = context.lookup("java:global/classes/EquipmentProviderJPA");
			//res = context.lookup(name);
			res = context.lookup("java:global/NetBuilder-Web/" + name);
			//"java:comp/env/"
			System.out.println("Bean java:global/NetBuilder-Web/" + name + " found!!!");
		} catch (NamingException e) {
			System.err.println("Bean java:global/NetBuilder-Web/" + name + " not found!");
			e.printStackTrace();
		}		
		return res;
	}	
}
